//
//  MBDGroupQRCodeCell.swift
//  MasterBoy
//
//  Created by kyson on 2024/8/27.
//

import Foundation

import SnapKit
import Kingfisher
import Dispatch


class MBDGroupQRCodeCell : MBDCommonCell {
    var groupQRCodeImageView : UIImageView!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.initialize()
    }
    
    override var cellModel: Any!{
        didSet {
            let realCellModel = self.cellModel as! MBDGroupQRCodeCellModel
            
            self.groupQRCodeImageView.image = UIImage(named: "ico_loading_01")

            //异步加载
            DispatchQueue.global().async {
                
                let qrCode = QRCodeUtils.qrcodeWithUrl(groupUrl: realCellModel.groupQRCodeURL)
                DispatchQueue.main.async {
                    self.groupQRCodeImageView.image = qrCode
                }
            }
            
            groupQRCodeImageView.snp.updateConstraints { make in
                make.height.equalTo(realCellModel.cellHeight())
                make.width.equalTo(realCellModel.cellHeight() )
            }
        }
    }

    func initialize() {
        
        //暂不支持暗黑模式，所有背景颜色都写死
        self.commonBackgroundView.setDynamicForgroundColor()

        self.contentView.setDynamicForgroundColor()
        self.separateLine.isHidden = true

        groupQRCodeImageView = UIImageView()
        groupQRCodeImageView.layer.cornerRadius = 4
        groupQRCodeImageView.clipsToBounds = true
        self.contentView.addSubview(groupQRCodeImageView)
        groupQRCodeImageView.snp.makeConstraints { make in
            make.height.equalTo(0)
            make.width.equalTo(0)
            make.center.equalToSuperview()
        }
    }
}
